//
//  SCFilterTextField.m
//  Shuaiche
//
//  Created by zzs on 2018/6/8.
//  Copyright © 2018年 zzs. All rights reserved.
//

#import "SCFilterTextField.h"

@implementation SCFilterTextField

//这个方法不能少
- (BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)paste:(id)sender{
    UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
    NSString *content = [pasteboard string];
    if(self.keyboardType == UIKeyboardTypeDecimalPad){
        return;
    }else if (self.keyboardType == UIKeyboardTypeNumberPad){
        if(self.filterTextFieldType!= SCFilterTextFieldPhoneNum)return;
        NSString *phone = [content stringByReplacingOccurrencesOfString:@"+86" withString:@""];
      phone = [[phone componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
        if (!phone) {
            return;
        }
        UITextRange *selecteRange = [self selectedTextRange];
        NSInteger startOffset = [self offsetFromPosition:self.beginningOfDocument toPosition:selecteRange.start];
        NSInteger endOffset = [self offsetFromPosition:self.beginningOfDocument toPosition:selecteRange.end];
        NSRange range = NSMakeRange(startOffset, endOffset-startOffset);
           self.text =  [self.text stringByReplacingCharactersInRange:range withString:phone];
    }else{
        [super paste:sender];
    }
}

@end
